package com.bw.module_motorcircle.test_recyclerview.diffi

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bw.lib_image.GlideApp
import com.bw.lib_widget.test.MyView
import com.bw.module_motorcircle.R
import com.bw.module_motorcircle.databinding.ItemTestBinding
import com.bw.module_motorcircle.test_recyclerview.MyModel
import kotlin.math.log

/**
 * @author ytx
 * @date 2025-09-24 13:48
 * @description
 */
class MyDiffAdapter(
    val onImageClick:(position:Int)->Unit = {},
):RecyclerView.Adapter<MyDiffAdapter.MyViewHolder>(){
    companion object{
        private const val TAG = "MyAdapter"
    }
    private val list = ArrayList<MyModel>()
    fun setData(mList: List<MyModel>){
        val calculateDiff = DiffUtil.calculateDiff(MyDiffCallBack(list.toList(), mList))
        list.clear()
        list.addAll(mList)
        calculateDiff.dispatchUpdatesTo(this) //代替notifyDataSetChanged

    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int, payloads: MutableList<Any>) {
        if (payloads.isEmpty()){
            onBindViewHolder(holder,position) //刷新整个item条目
        }else{
            if (payloads.contains("like")){
                Log.d(TAG, "onBindViewHolder只刷新爱心: "+position)
                holder.binding.ivLike.isEnabled = list[position].like
            }
            if (payloads.contains("title")){
                Log.d(TAG, "onBindViewHolder只刷新标题: "+position)
                holder.binding.tv.text = list[position].title
            }
            if (payloads.contains("url")){
                Log.d(TAG, "onBindViewHolder只刷新图片: "+position)
                GlideApp.with(holder.binding.iv)
                    .load(list[position].url)
                    .into(holder.binding.iv)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, position: Int): MyViewHolder {
        Log.d(TAG, "onCreateViewHolder: "+position)
        val binding = DataBindingUtil.inflate<ItemTestBinding>(LayoutInflater.from(parent.context), R.layout.item_test,parent,false)
        return MyViewHolder(binding,onImageClick) //事件回调
    }

    override fun getItemCount(): Int {
       return list.size
    }
    override fun onBindViewHolder(viewHolder: MyViewHolder, position: Int) {
        Log.d(TAG, "onBindViewHolder: "+position)
        viewHolder.binding.myModel = list[position]
        viewHolder.binding.ivLike.isEnabled = list[position].like

        //item点击事件:创建多个匿名对象，占用内存，极端情况内存抖动
//        viewHolder.binding.root.setOnClickListener{
//
//        }
        //不要做耗时操作，卡顿---》ANR
//        Thread.sleep(5000)

    }

    class MyViewHolder(
        val binding:ItemTestBinding,
        val onImageClick:(position:Int)->Unit = {},
    ):RecyclerView.ViewHolder(binding.root){
        init {
            //设置点击事件，不会创建多个匿名对象
            binding.iv.setOnClickListener {
                onImageClick.invoke(adapterPosition)
            }
        }
    }

}